/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 * 
 */
package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.Modulo;


/**
 * <p>
 * Implementacion de los metodos genericos de acceso a datos, necesarios para la gestion de Modulos de los 
 * Comerciantes en SISDEP.
 * </p>
 * <p>
 * Copyright (c) 2012<BR>
 * Consult-Soft S.A.<BR>
 * Created on: May 01, 2012<BR>
 * 
 * @author atabares
 * </p>
 */
public class ModuloDAOImpl extends JdbcDaoSupport implements IModuloDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM GES_MODULO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(Modulo modulo) {
		deleteOperation.execute( modulo  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public Modulo findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(Modulo modulo) {
		insertOperation.execute( modulo );
	}

	public void update(Modulo modulo) {
		updateOperation.execute( modulo );
	}

	public Modulo findByCriteria(Modulo  modulo) {
		return findByCriteriaQuery.runQuery(modulo);
	}

	public List load() {
		return findAllQuery.runQuery();
	}

	public Modulo findByActiveState(Modulo  modulo) {
		return findByActiveStateQuery.runQuery(modulo);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_MODULO_T ("
				  + " ID, "
				  + " CODIGO, "
				  + " ID_MODULO_DISENO, "
				  + " DIRECCION_MODULO, "
				  + " CODIGO_BARRIO, "
				  + " BARRIO, "
				  + " CODIGO_COMUNA, "
				  + " COMUNA, "
				  + " ESTADO_FINANCIERO, "
				  + " VALOR_CONTRAPRESTACION, "
				  + " VALOR_MORA, "
				  + " FACTURA, "
				  + " TIPO_ENERGIA, "
				  + " PLAQUETA_HACIENDA, "
				  + " DESCRIPCION, "
				  + " ESTADO, "
				  + " CONTRATO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " GES_MODULO_SEC.nextVal, "
			  	  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(Modulo modulo) {
			InsertOperation.this.update( new Object[]{
				modulo.getCodigo(),
				modulo.getIdModuloDiseno(),
				modulo.getDireccionModulo(),
				modulo.getCodigoBarrio(),
				modulo.getBarrio(),
				modulo.getCodigoComuna(),
				modulo.getComuna(),
				modulo.getEstadoFinanciero(),
				modulo.getValorContraprestacion(),
				modulo.getValorMora(),
				modulo.getFactura(),
				modulo.getTipoEnergia(),
				modulo.getPlaquetaHacienda(),
				modulo.getDescripcion(),
				modulo.getEstado(),
				modulo.getContrato(),
				modulo.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_MODULO_T SET "
					  + " CODIGO = ? ,"
					  + " ID_MODULO_DISENO = ? ,"
					  + " DIRECCION_MODULO = ? ,"
					  + " CODIGO_BARRIO = ? ,"
					  + " BARRIO = ? ,"
					  + " CODIGO_COMUNA = ? ,"
					  + " COMUNA = ? ,"
					  + " ESTADO_FINANCIERO = ? ,"
					  + " VALOR_CONTRAPRESTACION = ? ,"
					  + " VALOR_MORA = ? ,"
					  + " FACTURA = ? ,"
					  + " TIPO_ENERGIA = ? ,"
					  + " PLAQUETA_HACIENDA = ? ,"
					  + " DESCRIPCION = ? ,"
					  + " ESTADO = ? ,"
					  + " CONTRATO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + " WHERE ID = ?";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			
			compile();
		}
		public void execute(Modulo modulo) {
			UpdateOperation.this.update( new Object[]{
				modulo.getCodigo(),
				modulo.getIdModuloDiseno(),
				modulo.getDireccionModulo(),
				modulo.getCodigoBarrio(),
				modulo.getBarrio(),
				modulo.getCodigoComuna(),
				modulo.getComuna(),
				modulo.getEstadoFinanciero(),
				modulo.getValorContraprestacion(),
				modulo.getValorMora(),
				modulo.getFactura(),
				modulo.getTipoEnergia(),
				modulo.getPlaquetaHacienda(),
				modulo.getDescripcion(),
				modulo.getEstado(),
				modulo.getContrato(),
				modulo.getUsuarioModifica(),
				modulo.getId()
			});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_MODULO_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(Modulo modulo) {
			this.update( new Object[]{
				modulo.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_MODULO_T "
										+ " WHERE ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			Modulo modulo = new Modulo();
				modulo.setId(new Long(rs.getLong("ID")));
				modulo.setCodigo(rs.getString("CODIGO"));
				modulo.setIdModuloDiseno(new Long(rs.getLong("ID_MODULO_DISENO")));
				modulo.setDireccionModulo(rs.getString("DIRECCION_MODULO"));
				modulo.setCodigoBarrio(rs.getString("CODIGO_BARRIO"));
				modulo.setBarrio(rs.getString("BARRIO"));
				modulo.setCodigoComuna(rs.getString("CODIGO_COMUNA"));
				modulo.setComuna(rs.getString("COMUNA"));
				modulo.setEstadoFinanciero(rs.getString("ESTADO_FINANCIERO"));
				modulo.setValorContraprestacion(new Long(rs.getLong("VALOR_CONTRAPRESTACION")));
				modulo.setValorMora(new Long(rs.getLong("VALOR_MORA")));
				modulo.setFactura(rs.getString("FACTURA"));
				modulo.setTipoEnergia(new Long(rs.getLong("TIPO_ENERGIA")));
				modulo.setPlaquetaHacienda(rs.getString("PLAQUETA_HACIENDA"));
				modulo.setEstado(rs.getString("ESTADO"));
				modulo.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				modulo.setFechaCrea(rs.getDate("FECHA_CREA"));		
				modulo.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				modulo.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
				modulo.setDescripcion(rs.getString("DESCRIPCION"));	
				modulo.setContrato(rs.getString("CONTRATO"));
			return modulo;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
					  					+ " ID = ? ";
    	
		public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			
			compile();
    	}
    	
		public Modulo runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			
			if ( result.size() > 0 ) {
        		return (Modulo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND ID_MODULO_DISENO = ? "
			  	  	  					//+ " AND DIRECCION_MODULO = ? "
			  	  	  					//+ " AND CODIGO_BARRIO = ? "
			  	  	  					//+ " AND BARRIO = ? "
			  	  	  					//+ " AND CODIGO_COMUNA = ? "
			  	  	  					//+ " AND COMUNA = ? "
			  	  	  					//+ " AND ESTADO_FINANCIERO = ? "
			  	  	  					//+ " AND VALOR_CONTRAPRESTACION = ? "
			  	  	  					//+ " AND VALOR_MORA = ? "
			  	  	  					//+ " AND FACTURA = ? "
			  	  	  					//+ " AND TIPO_ENERGIA = ? "
			  	  	  					//+ " AND PLAQUETA_HACIENDA = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
			  	  	  					//+ " AND DESCRIPCION = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public Modulo runQuery(Modulo modulo) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//modulo.getId(),
				//modulo.getCodigo(),
				//modulo.getIdModuloDiseno(),
				//modulo.getDireccionModulo(),
				//modulo.getCodigoBarrio(),
				//modulo.getBarrio(),
				//modulo.getCodigoComuna(),
				//modulo.getComuna(),
				//modulo.getEstadoFinanciero(),
				//modulo.getValorContraprestacion(),
				//modulo.getValorMora(),
				//modulo.getFactura(),
				//modulo.getTipoEnergia(),
				//modulo.getPlaquetaHacienda(),
				//modulo.getEstado(),
				//modulo.getUsuarioCrea(),
				//modulo.getFechaCrea(),
				//modulo.getUsuarioModifica(),
				//modulo.getFechaModifica(),
				//modulo.getDescripcion(),
				});
			if ( result.size() > 0 ) {
        		return (Modulo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ " WHERE ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public Modulo runQuery(Modulo modulo) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (Modulo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}